package com.shang.service.impl;

import com.shang.dao.CommentDao;
import com.shang.dao.ResultDao;
import com.shang.entity.Comment;
import com.shang.entity.Result;
import com.shang.entity.UserMark;
import com.shang.service.ResultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ResultServiceImpl implements ResultService {

    @Autowired
    private ResultDao resultDao;

    @Autowired
    private CommentDao commentDao;
    @Override
    public List<UserMark> getUserMark(String userName) {
        List<UserMark> userMarkList = resultDao.getUserMark(userName);
        return userMarkList;
    }

    @Override
    public int updateSolved(long comment_id) {
        int updateNum = resultDao.updateSolved(comment_id);
        return updateNum;
    }

    @Override
    public String deleteUserMark(long comment_id) {
        boolean solved = resultDao.getSolved(comment_id);
        if (solved){
            int deleteNum = resultDao.deleteUserMark(comment_id);
            int i = commentDao.updateCommentMark(comment_id,0);//取消关注
            if (deleteNum==1&&i==1){
                return "删除成功！";
            }
            else {
                return "删除失败！";
            }
        }
        else {
            return "未解决,无法删除";
        }
    }

    @Override
    public int addResultScore(String date,int beforeValue,int afterValue) {
        boolean haveResult = resultDao.haveResult(date);
        if (haveResult){
            int updateResult = resultDao.updateResult(date, beforeValue, afterValue);
            return updateResult;
        }
        else {
            resultDao.addResult(date);
            int updateResult = resultDao.updateResult(date, beforeValue, afterValue);
            return updateResult;
        }

    }

    @Override
    public Result getResultByDate(String date) {
        Result resultByDate = resultDao.getResultByDate(date);
        return resultByDate;
    }


}
